package cn.autumnorange.app.user.provider.repository.impl;

import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.common.repository.impl.BaseDateRepositoryImpl;
import cn.autumnorange.app.user.api.dto.GramtedAuthorityImpl;
import cn.autumnorange.app.user.provider.entity.SysRoleEntity;
import cn.autumnorange.app.user.provider.entity.middle.SysRoleUserEntity;
import cn.autumnorange.app.user.provider.repository.SysRoleRepository;
import org.hibernate.transform.Transformers;
import org.hibernate.type.LongType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wsf
 * @time 2019/6/6 21:20
 */
@Repository
public class SysRoleRepositoryImpl extends BaseDateRepositoryImpl<SysRoleEntity>
    implements SysRoleRepository {
  @Override
  public List<SysRoleEntity> findRolesByRoleIdList(Map<String, Object> paramMap) {
    String sql = null;
    for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
      sql = "SELECT r.* from sys_role r where r.id in (:" + entry.getKey() + ")";
      break;
    }
    Map<String, Type> returnType = new HashMap<>();
    returnType.put("id", new LongType());
    returnType.put("name", new StringType());
    return findByNativeQuery(sql, paramMap, returnType);
  }

  @Override
  public ResultDto<List<GramtedAuthorityImpl>> getRolesByUserId(long userId) {
    List<SysRoleEntity> resRoleList = null;
    Map<String, Object> paramMap = new HashMap();
    paramMap.put("userId", userId);
    String sql = "SELECT ru.* from sys_role_user ru where ru.userId=:userId";
    Map<String, Type> returnType = new HashMap<>();
    returnType.put("userId", new LongType());
    returnType.put("roleId", new LongType());
    List<SysRoleUserEntity> resRoleUser =
        findByNativeQuery(
            sql, paramMap, Transformers.aliasToBean(SysRoleUserEntity.class), returnType);
    if (resRoleUser != null && resRoleUser.size() > 0) {
      paramMap.clear();
      List<Long> paramList = new ArrayList();
      for (SysRoleUserEntity roleUser : resRoleUser) {
        paramList.add(roleUser.getRoleId());
      }
      paramMap.put("roleIdList", paramList);
      //      sql = "SELECT r.id,r.name from sys_role r where r.id in (:roleIdList)";
      resRoleList = findRolesByRoleIdList(paramMap);
      //      查询该用户所有权限所有菜单
      //            if (resRoleList != null && resRoleList.size() > 0) {
      //                user.setRoles(resRoleList);
      //                return null;
      //            }
      if (resRoleList.size() > 0) {
        List<GramtedAuthorityImpl> sysRoleDtos = new ArrayList<GramtedAuthorityImpl>();
        for (SysRoleEntity sysRoleEntity : resRoleList) {
          GramtedAuthorityImpl sysRoleDto = new GramtedAuthorityImpl();
          sysRoleDto.setAuthority(sysRoleEntity.getAuthority());
          sysRoleDtos.add(sysRoleDto);
        }
        return ResultDto.createDataResult(sysRoleDtos);
      }
    }

    return null;
  }
}
